using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PlugNT.Safe;
using PlugNT.Database.Common;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    public class GroupAdmin
    {


        private static string currTableName = WebConfig.TablePrefix + "group_admin";








        public GroupAdminInfo GetGroupAdminInfo(string power)
        {
            GroupAdminInfo model = null;
            IDataReader dr = GetReader(1, "*", " and [power]='" + StringHelper.SqlFilter(power) + "'");
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new GroupAdminInfo();
                    model.id = int.Parse(dr["id"].ToString());
                    model.power = dr["power"].ToString();
                    model.name = dr["name"].ToString();
                    model.code_no_list = dr["code_no_list"].ToString();
                }
                dr.Close();
            }
            return model;
        }

        public string GetName(string power)
        {
            string sql = "Select top 1 [name] From " + currTableName + " where [power]='" + StringHelper.SqlFilter(power) + "'";
            object obj = DbHelper.GetSingle(sql);
            return (obj == null) ? "" : obj.ToString();
        }

        public string GetCodeList(string power)
        {
            string sql = "Select top 1 code_no_list From " + currTableName + " where [power]='" + StringHelper.SqlFilter(power) + "'";
            object obj = DbHelper.GetSingle(sql);
            return (obj == null) ? "" : obj.ToString();
        }










        public bool Add(GroupAdminInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName);
            strSql.Append(" ([power],[name],[code_no_list]) values (");
            strSql.Append("'" + StringHelper.SqlFilter(model.power) + "',");
            strSql.Append("'" + StringHelper.SqlFilter(model.name) + "',");
            strSql.Append("'" + StringHelper.SqlFilter(model.code_no_list) + "'");
            strSql.Append(")");

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }






        public bool Update(GroupAdminInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + currTableName);
            strSql.Append(" set [name]='" + StringHelper.SqlFilter(model.name) + "',");
            strSql.Append("[code_no_list]='" + StringHelper.SqlFilter(model.code_no_list) + "'");
            strSql.Append(" where [power]='" + StringHelper.SqlFilter(model.power) + "' ");

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }






        public bool Delete(string power)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName);
            strSql.Append(" where [power]= '" + StringHelper.SqlFilter(power) + "'");
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName);
            strSql.Append(" where [id]= "+id.ToString());
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }











        private IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            string sql = "select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere;
            return DbHelper.ExecuteReader(sql);
        }






        public DataTable GetList(string andwhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from " + currTableName);
            strSql.Append(" where 1=1 " + andwhere);
            return DbHelper.GetDataTable(strSql.ToString());
        }

        public DataTable GetPowerList(string andwhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [power],[name] from " + currTableName);
            strSql.Append(" where 1=1 " + andwhere);
            return DbHelper.GetDataTable(strSql.ToString());
        }
    }
}

